$OpenBSD: patch-daemon_gdm-server_c,v 1.6 2015/04/03 18:39:06 ajacoutot Exp $

REVERT - OpenBSD lacks sigwaitinfo(2)
From 956d7d1c7a0cfbf2beacdb9e88e645e15ad32047 Mon Sep 17 00:00:00 2001
From: Jasper St. Pierre <jstpierre@mecheye.net>
Date: Fri, 14 Feb 2014 19:32:50 +0000
Subject: server: Process SIGUSR1 more carefully

--- daemon/gdm-server.c.orig	Tue Mar 10 12:46:40 2015
+++ daemon/gdm-server.c	Fri Apr  3 20:24:53 2015
@@ -94,6 +94,7 @@ struct GdmServerPrivate
         char    *auth_file;
 
         guint    child_watch_id;
+        guint    sigusr1_id;
 
         gboolean is_initial;
 };
@@ -179,9 +180,12 @@ gdm_server_get_display_device (GdmServer *server)
         return g_strdup (server->priv->display_device);
 }
 
-static void
-gdm_server_ready (GdmServer *server)
+static gboolean
+on_sigusr1 (gpointer user_data)
+
 {
+        GdmServer *server = user_data;
+
         g_debug ("GdmServer: Got USR1 from X server - emitting READY");
 
         gdm_run_script (GDMCONFDIR "/Init", GDM_USERNAME,
@@ -190,75 +194,10 @@ gdm_server_ready (GdmServer *server)
                         server->priv->auth_file);
 
         g_signal_emit (server, signals[READY], 0);
+        return FALSE;
 }
 
-static GSList *active_servers;
-static gboolean sigusr1_thread_running;
-static GCond sigusr1_thread_cond;
-static GMutex sigusr1_thread_mutex;
-
-static gboolean
-got_sigusr1 (gpointer user_data)
-{
-        GPid pid = GPOINTER_TO_UINT (user_data);
-        GSList *l;
-
-        g_debug ("GdmServer: got SIGUSR1 from PID %d", pid);
-
-        for (l = active_servers; l; l = l->next) {
-                GdmServer *server = l->data;
-
-                if (server->priv->pid == pid)
-                        gdm_server_ready (server);
-        }
-
-        return G_SOURCE_REMOVE;
-}
-
-static gpointer
-sigusr1_thread_main (gpointer user_data)
-{
-        sigset_t sigusr1_mask;
-
-        /* Handle only SIGUSR1 */
-        sigemptyset (&sigusr1_mask);
-        sigaddset (&sigusr1_mask, SIGUSR1);
-        sigprocmask (SIG_SETMASK, &sigusr1_mask, NULL);
-
-        g_mutex_lock (&sigusr1_thread_mutex);
-        sigusr1_thread_running = TRUE;
-        g_cond_signal (&sigusr1_thread_cond);
-        g_mutex_unlock (&sigusr1_thread_mutex);
-
-        /* Spin waiting for a SIGUSR1 */
-        while (TRUE) {
-                siginfo_t info;
-
-                if (sigwaitinfo (&sigusr1_mask, &info) == -1)
-                        continue;
-
-                g_idle_add (got_sigusr1, GUINT_TO_POINTER (info.si_pid));
-        }
-
-        return NULL;
-}
-
 static void
-gdm_server_launch_sigusr1_thread_if_needed (void)
-{
-        static GThread *sigusr1_thread;
-
-        if (sigusr1_thread == NULL) {
-                sigusr1_thread = g_thread_new ("gdm SIGUSR1 catcher", sigusr1_thread_main, NULL);
-
-                g_mutex_lock (&sigusr1_thread_mutex);
-                while (!sigusr1_thread_running)
-                        g_cond_wait (&sigusr1_thread_cond, &sigusr1_thread_mutex);
-                g_mutex_unlock (&sigusr1_thread_mutex);
-        }
-}
-
-static void
 gdm_server_init_command (GdmServer *server)
 {
         gboolean debug = FALSE;
@@ -276,7 +215,7 @@ gdm_server_init_command (GdmServer *server)
                 debug_options = "";
         }
 
-#define X_SERVER_ARG_FORMAT " -background none -noreset -verbose %s%s"
+        #define X_SERVER_ARG_FORMAT " -background none -noreset -verbose %s%s"
 
 #ifdef WITH_SYSTEMD
 
@@ -709,12 +648,6 @@ server_child_watch (GPid       pid,
         g_object_unref (server);
 }
 
-static void
-prune_active_servers_list (GdmServer *server)
-{
-        active_servers = g_slist_remove (active_servers, server);
-}
-
 static gboolean
 gdm_server_spawn (GdmServer    *server,
                   const char   *vtarg,
@@ -752,15 +685,6 @@ gdm_server_spawn (GdmServer    *server,
         g_debug ("GdmServer: Starting X server process: %s", freeme);
         g_free (freeme);
 
-        active_servers = g_slist_append (active_servers, server);
-
-        g_object_weak_ref (G_OBJECT (server),
-                           (GWeakNotify)
-                           prune_active_servers_list,
-                           server);
-
-        gdm_server_launch_sigusr1_thread_if_needed ();
-
         if (!g_spawn_async_with_pipes (NULL,
                                        argv,
                                        (char **)env->pdata,
@@ -1096,11 +1020,16 @@ gdm_server_class_init (GdmServerClass *klass)
 static void
 gdm_server_init (GdmServer *server)
 {
+
         server->priv = GDM_SERVER_GET_PRIVATE (server);
 
         server->priv->pid = -1;
 
         server->priv->log_dir = g_strdup (LOGDIR);
+
+        server->priv->sigusr1_id = g_unix_signal_add (SIGUSR1,
+                                                      on_sigusr1,
+                                                      server);
 }
 
 static void
@@ -1114,6 +1043,9 @@ gdm_server_finalize (GObject *object)
         server = GDM_SERVER (object);
 
         g_return_if_fail (server->priv != NULL);
+
+        if (server->priv->sigusr1_id > 0)
+                g_source_remove (server->priv->sigusr1_id);
 
         gdm_server_stop (server);
 
